package com.jwell.panprovider.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panprovider.contract.ProviderRegisterContract
import com.jwell.panprovider.entity.AppCocntractDetailDto
import com.jwell.panprovider.entity.ValMsgDto
import com.jwell.panprovider.model.ProviderRegisterModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/22.
 * 供应预登记
 */
class ProviderRegisterPresenter : ProviderRegisterContract.Presenter, BasePresenter<ProviderRegisterContract.View>() {


    private val model by lazy {
        ProviderRegisterModel()
    }


    override fun appGetContractDetail(data: ProviderRegisterModel.Detail) {
        model.appGetContractDetail(data, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                var data = Gson().fromJson<AppCocntractDetailDto>(data.getString("result"), object : TypeToken<AppCocntractDetailDto>() {
                }.type)
                getView()?.setDetail(data)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
            }
        })
    }

    override fun checkCarNum(id: String) {
        model.checkCarNum(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                var data = Gson().fromJson<ValMsgDto>(data.getString("result"), object : TypeToken<ValMsgDto>() {
                }.type)
                getView()?.showCheckCarNum(data)
            }

            override fun onFailed(msg: String) {
                getView()?.toast(msg)
            }
        })
    }

    override fun checkDriverPhone(id: String, name: String?) {
        model.checkDriverPhone(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                if (data.getBoolean("result")) {
                    getView()?.showCheckDriverPhone(false, id, name)
                } else {
                    getView()?.showCheckDriverPhone(true, id, name)
                }
            }

            override fun onFailed(msg: String) {
                getView()?.toast(msg)
            }

        })
    }

    override fun appSupplyRegistration(data: ProviderRegisterModel.RequestData) {
        model.appSupplyRegistration(data, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getView()?.toast("登记成功")
                getView()?.registerSuccess()
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }
}